package com.example.barcode2ds;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.panlin.scan.Barcode2D;
import com.panlin.scan.IBarcodeResult;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, IBarcodeResult {

    private String TAG = "MainActivity_2D";

    private Barcode2D barcode2D;
    private Button btnScan;
    private Button btnStop;
    private Button btnOpen;//add by hejiang
    private Button btnClose;//add by hejiang
    private Button btnstartContinue;
    private Button btnstopContinue;
    private TextView tvData;
    private Context context;
    private ReadThread readThread;
    private boolean isRunRead = false;
    private boolean isOpen = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;
        barcode2D = new Barcode2D(this);
        tvData = (TextView) findViewById(R.id.tvData);
        btnScan = (Button) findViewById(R.id.btnScan);
        btnStop = (Button) findViewById(R.id.btnStop);
        btnScan.setOnClickListener(this);
        btnStop.setOnClickListener(this);
        btnOpen = (Button) findViewById(R.id.btnOpen);
        btnClose = (Button) findViewById(R.id.btnClose);
        btnOpen.setOnClickListener(this);
        btnClose.setOnClickListener(this);
        btnstartContinue = (Button) findViewById(R.id.btnstartContinue);
        btnstopContinue = (Button) findViewById(R.id.btnstopContinue);
        btnstartContinue.setOnClickListener(this);
        new InitTask().execute();// open();
    }

    @Override
    protected void onDestroy() {
        Log.i(TAG, "onDestroy");
        close();
        super.onDestroy();
        android.os.Process.killProcess(Process.myPid());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnOpen:
                open();
                isOpen = true;
                btnOpen.setBackgroundResource(R.drawable.button_press);
                break;
            case R.id.btnClose:
                close();
                isOpen = false;
                if (readThread != null) {
                    isRunRead = false;
                    Log.d(TAG, "closeScan 111");
                    readThread.interrupt();
                }
                btnClose.setBackgroundResource(R.drawable.button_press);
                btnOpen.setBackgroundResource(R.drawable.button_nopress);
                btnScan.setBackgroundResource(R.drawable.button_nopress);
                btnStop.setBackgroundResource(R.drawable.button_nopress);
                btnstartContinue.setBackgroundResource(R.drawable.button_nopress);
                btnstopContinue.setBackgroundResource(R.drawable.button_nopress);
                break;
            case R.id.btnScan:
                if(isOpen) {
                    openScan();
                    btnScan.setBackgroundResource(R.drawable.button_press);
                }else{
                    Toast.makeText(context, R.string.open_notice, Toast.LENGTH_SHORT).show();
                    btnScan.setBackgroundResource(R.drawable.button_nopress);
                }
                break;
            case R.id.btnStop:
                if(isOpen) {
                    closeScan();
                    btnStop.setBackgroundResource(R.drawable.button_press);
                }else{
                    Toast.makeText(context, R.string.open_notice, Toast.LENGTH_SHORT).show();
                    btnStop.setBackgroundResource(R.drawable.button_nopress);
                }

                break;
            case R.id.btnstartContinue:
                if(isOpen) {
                    btnstartContinue.setBackgroundResource(R.drawable.button_press);
                    readThread = new ReadThread();
                    isRunRead = true;
                    //enableBlockScankey();
                    readThread.start();
                }else{
                    Toast.makeText(context, R.string.open_notice, Toast.LENGTH_SHORT).show();
                    btnstartContinue.setBackgroundResource(R.drawable.button_nopress);
                }
                break;

            case R.id.btnstopContinue:
                btnstopContinue.setBackgroundResource(R.drawable.button_press);
                btnOpen.setBackgroundResource(R.drawable.button_nopress);
                btnClose.setBackgroundResource(R.drawable.button_nopress);
                btnScan.setBackgroundResource(R.drawable.button_nopress);
                btnStop.setBackgroundResource(R.drawable.button_nopress);
                btnstartContinue.setBackgroundResource(R.drawable.button_nopress);
                closeScan();
                isOpen = false;
                if (readThread != null) {
                    isRunRead = false;
                    Log.d(TAG, "closeScan 222");
                    readThread.interrupt();
                }
                break;
        }
    }

    @Override
    public void getBarcode(String barcode) {
        Log.d(TAG,tvData.getLineCount() + "");
        if (tvData.getLineCount() > 25) {
            tvData.setText("");
        }
        //tvData.setText(barcode);
        tvData.append(barcode);
        tvData.append("\n");
    }

    public class InitTask extends AsyncTask<String, Integer, Boolean> {
        ProgressDialog mypDialog;

        @Override
        protected Boolean doInBackground(String... params) {
            // TODO Auto-generated method stub
            try {
               // open();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            mypDialog.cancel();
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            mypDialog = new ProgressDialog(MainActivity.this);
            mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mypDialog.setMessage("init...");
            mypDialog.setCanceledOnTouchOutside(false);
            mypDialog.setCancelable(false);
            mypDialog.show();
        }
    }

    private class ReadThread extends Thread {
        @Override
        public void run() {
            while (isRunRead) {
                Log.d(TAG, "ReadThread 111" );
                try {
                    openScan();
                    Thread.sleep(100);
                } catch (Exception e) {
                    // Log.e(TAG, "ReadThread," + e.getMessage());
                }
            }
        }
    }
    //开始扫码
    public void openScan() {
        barcode2D.startScan(context); // getApplicationContext()
    }

    //停止扫描
    public void closeScan() {
        barcode2D.stopScan(context); // getApplicationContext()
    }

    //打开
    public void open() {
        barcode2D.open(context/*getApplicationContext()*/, this);
    }

    //关闭扫描
    public void close() {
        barcode2D.stopScan(context/*getApplicationContext()*/);
        barcode2D.close(context/*getApplicationContext()*/);
    }

    //开启连续扫描
    public void startContinuousScan()
    {
        barcode2D.enableContinuousScan(true);
    }

    //关闭连续扫描
    public void stopContinuousScan()
    {
        barcode2D.enableContinuousScan(false);
    }

    //设置扫描输出模式
    public void setOutScanMode(int mode)
    {
        barcode2D.setOutScanMode(mode);
    }

    //开启 拦截扫描键
    public void enableBlockScankey()
    {
        barcode2D.enableBlockScankey(true);
    }
    //关闭 拦截扫描键
    public void disableBlockScankey()
    {
        barcode2D.enableBlockScankey(false);
    }

    //设置声音输出开关
    public void setScanSound(boolean on)
    {
        barcode2D.setScanSound(on);
    }
    //设置振动输出开关
    public void setScanVibrator(boolean on)
    {
        barcode2D.setScanVibrator(on);
    }
    //设置回车输出开关
    public void setScanEnterKey(boolean on)
    {
        barcode2D.setScanEnterKey(on);
    }
    //设置前缀字符
    public void setScanPrefix(String prefix)
    {
        barcode2D.setScanPrefix(prefix);
    }
    //设置后缀字符
    public void setScanSuffix(String suffix)
    {
        barcode2D.setScanSuffix(suffix);
    }
    //设置扫描Laser模式
    public void setScanLaserMode(int mode)
    {
        barcode2D.setScanLaserMode(mode);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e("keyCode", "keyCode"+keyCode+" ,event:"+event.getKeyCode());
        return super.onKeyDown(keyCode, event);
    }
}
